package cn.catsdad.base.controller;

import cn.catsdad.base.entity.BaseCrudEntity;
import cn.catsdad.base.page.PageParam;
import cn.catsdad.base.service.api.IBaseCrudService;
import cn.catsdad.base.web.HttpResponse;
import cn.catsdad.util.StringUtil;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.Collection;

/**
 * @author xuyl
 * @version 1.0 2024/8/29
 */
@RestController
public class BaseCrudController<T extends BaseCrudEntity, ID extends Serializable, S extends IBaseCrudService<T, ID>, P extends PageParam> extends BaseController<T, ID, S, P> {

    @Override
    public HttpResponse<T> queryByKey(ID id) {
        HttpResponse<T> result = new HttpResponse<>();
        if (id != null) {
            result.setData(service.queryByIdWithIsDeletedFalse(id));
        }
        return result;
    }

    @Override
    public HttpResponse<Integer> delete(Collection<ID> ids) {
        HttpResponse<Integer> result = new HttpResponse<>();
        if (StringUtil.isNotEmpty(ids)) {
            service.logicDeleteByIds( ids);
            result.setData(ids.size());
        }
        return result;
    }
}
